﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UniversalDataAccess.BusinessEntities;

namespace UniversalDataAccess.DataAccess
{
    public class FacultyDataAccess : EducationalDivisionsDataAccess
    {
        public FacultyDataAccess(String dbName, String conn, Boolean usePredefinedMetadata)
            : base(dbName, conn, usePredefinedMetadata)
        {
        }

        public List<Faculty> SelectFacultiesList()
        {
            List<EducationalDivision> divList = SelectEducationalDivisionsList();

            List<FACULTY> facList = dataContext.FACULTY.ToList();

            //Merge result
            var res = (from div in divList
                       join fac in facList on div.ID equals fac.ID
                       select new Faculty
                       {
                           DataBaseName = dataBaseName,
                           CLASS_ID = div.CLASS_ID,
                           EXT_ID = div.EXT_ID,
                           ID = div.ID,
                           NAME = div.NAME,
                           PARENT_ID = div.PARENT_ID,
                           BOSS_ID = div.BOSS_ID,
                           DATE_CREATED = div.DATE_CREATED,
                           DESCRIPTION = div.DESCRIPTION,
                           SPECIALITIES = fac.SPECIALITIES,
                           BUILDING = fac.BUILDING_ID
                       }).ToList();

            return res;
        }

        public Faculty SelectFacultyBy_Id(int Id)
        {
            try
            {
                EducationalDivision basePart = SelectEducationalDivisionBy_Id(Id);

                List<FACULTY> facPart = dataContext.FACULTY.ToList();

                //Merge result
                var res = (from fac in facPart
                           where fac.ID == basePart.ID
                           select new Faculty
                           {
                               DataBaseName = dataBaseName,
                               CLASS_ID = basePart.CLASS_ID,
                               EXT_ID = basePart.EXT_ID,
                               ID = basePart.ID,
                               NAME = basePart.NAME,
                               PARENT_ID = basePart.PARENT_ID,
                               BOSS_ID = basePart.BOSS_ID,
                               DATE_CREATED = basePart.DATE_CREATED,
                               DESCRIPTION = basePart.DESCRIPTION,
                               SPECIALITIES = fac.SPECIALITIES,
                               BUILDING = fac.BUILDING_ID
                           }).FirstOrDefault();

                var bld = dataContext.TOBJECT.Where(t => t.EXT_ID == res.BUILDING).FirstOrDefault();
                res.BUILDING_NAME = bld != null ? bld.NAME : String.Empty; 

                return res;
            }
            catch
            {
                throw new Exception("Error while SelectFacultyById");
            }
        }

        public void InsertFaculty(Faculty fac, ref int generated_Id)
        {
            try
            {
                InsertEducationalDivision(fac, ref generated_Id);

                int new_Id = generated_Id;
                FACULTY facPart = new FACULTY();
                facPart.DIVISION = dataContext.DIVISION.Where(t => t.ID == new_Id).FirstOrDefault();
                facPart.ID = new_Id;
                facPart.SPECIALITIES = fac.SPECIALITIES;
                facPart.BUILDING_ID = fac.BUILDING;

                //Submit changes
                dataContext.AddToFACULTY(facPart);
                dataContext.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new Exception("Error while InsertFaculty");
            }
        }

        public void UpdateFaculty(Faculty fac)
        {
            try
            {
                UpdateEducationalDivision(fac);

                FACULTY facPart = dataContext.FACULTY.Where(t => t.ID == fac.ID).FirstOrDefault();

                facPart.SPECIALITIES = fac.SPECIALITIES;
                facPart.BUILDING_ID = fac.BUILDING;

                //Submit changes
                dataContext.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new Exception("Error while UpdateFaculty");
            }
        }

        public void DeleteFaculty(Faculty fac)
        {
            try
            {
                FACULTY facPart = dataContext.FACULTY.Where(t => t.ID == fac.ID).FirstOrDefault();

                dataContext.DeleteObject(facPart);
                dataContext.SaveChanges();

                DeleteEducationalDivision(fac);
            }
            catch (Exception ex)
            {
                throw new Exception("Error while DeleteFaculty");
            }
        }
    }
}
